from domain.domain import User
from service.Message import Message,CodeMessageData


import re
class UserDAO():
    def __init__(self):
        print("UserDAO ")

    def add(self,session,oneUserData):
        """
        :param session: session
        :param oneUserData: one user data
        :return: Message Object
        """
        data=[]
        try:
            session.add(oneUserData)
            code=0000
            session.commit()
        except Exception as ex :
            exStr=str(ex)
            # print("是这里错了")
            # print(exStr)
            exArr=re.search("\([0-9]{4}.*\)",exStr)
            # print("code")
            # print(exArr)
            print(exArr[0])
            exTwo=exArr[0].replace("(","").split(',')[0]
            print('exTwo')
            print(exTwo)
            code=int(exTwo)
            session.rollback()
        if(code not in CodeMessageData):
            code=2001
        return  Message(code,CodeMessageData[code])

    def addAll(self,session,userList):
        """
        :param session: session
        :param oneUserData: one user data
        :return: Message Object
        """
        data=[]
        try:
            session.add_all(userList)
            code=0000
            session.commit()
        except Exception as ex :
            exStr=str(ex)
            exArr=re.search("\([0-9]{4}.*\)",exStr)
            print(exArr[0])
            exTwo=exArr[0].replace("(","").split(',')[0]
            print('exTwo')
            print(exTwo)
            code=int(exTwo)
            session.rollback()
        if(code not in CodeMessageData):
            code=2001
        return  Message(code,CodeMessageData[code])



    def deleteById(self,session,id):
        """
        :param session: session
        :param id: userid
        :return: result Message Object
        """
        data = []
        try:
            session.query(User).filter(User.id==id).delete()
            code = 0000
            session.commit()
        except Exception as ex :
            exStr=str(ex)
            exArr=re.search("\([0-9]{4}.*\)",exStr)
            print(exArr[0])
            exTwo=exArr[0].replace("(","").split(',')[0]
            print(exTwo)
            code=int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return  Message(code,CodeMessageData[code])


    def update(self,session,userData):
        """

        :param session:
        :param userData:
        :return:
        """
        code=0000
        try:
            session.commit()
        except Exception as e:
            print(e)
            print(e)
            code=1030
            session.rollback()
        return Message(code, CodeMessageData[code])

    def getInfoByJobNumber(self, session, jobNumber):
        """
        :param session: session
        :param jobNumber: jobNumber
        :return: Message object
        """
        code=0000
        data=[]
        try:
            info=session.query(User).filter(User.job_number == jobNumber).one()
            part={}
            part['job_number']=info.job_number
            part['user_name']=info.user_name
            part['access_level']=info.access_level
            data.append(part)
        except:
            code=1001
        return Message(code,CodeMessageData[code],data=data)

    def getOneInfoByJobNumber(self, session, jobNumber):
        """
        :param session: session
        :param jobNumber: jobNumber
        :return: Message object
        """
        code=0000
        data=[]
        try:
            info=session.query(User).filter(User.job_number == jobNumber).one()
            data.append(info)
        except:
            code=1001
        return Message(code,CodeMessageData[code],data=data)


    def getPAIdByJobNumber(self, session, jobNumber):
        """
        :param session:
        :param jobNumber:
        :return: Message object .data is  password acess_level id
        """
        data=[]
        try:
            code = 0000
            userData = session.query(User).filter(User.job_number == jobNumber).one()
            data.append(userData.password)
            data.append(userData.access_level)
            data.append(userData.id)
        except Exception as ex:
            code=1001
        return Message(code,CodeMessageData[code],data)

    def deleteByJobNumber(self,session,jobNumber):
        """
        delete user
        :param session:
        :param jobNumber:
        :return: Message object
        """
        try:
            code=0000
            session.query(User).filter(User.job_number == jobNumber).delete()
            session.commit()
        except Exception as ex:
            code=2001
            session.rollback()
        return Message(code, CodeMessageData[code])




    def deleteByIdlist(self, session, idList):
        """
              :param session: session
              :param id: personalTeamid
              :return: result Message Object
              """
        try:
            for each in idList:
                session.query(User).filter(User.id == each).delete()
            code = 0000
            session.commit()
        except Exception as ex:
            exStr = str(ex)
            exArr = re.search("\([0-9]{4}.*\)", exStr)
            print(exArr[0])
            exTwo = exArr[0].replace("(", "").split(',')[0]
            code = int(exTwo)
            session.rollback()
        if (code not in CodeMessageData):
            code = 2001
        return Message(code, CodeMessageData[code])






    def updateByDataList(self,session,newData):
        """
        update data
        :param session: session
        :param newData: newData
        :return:
        """
        code=0000
        try:
            for one in newData:
                part =session.query(User).filter(User.id == one.id).one()
                part.job_number=one.job_number
                part.user_name=one.user_name
                part.access_level=one.access_level
                part.password=one.password
            session.commit()
        except Exception as ex:
            # code=1003
            code=1019
            session.rollback()
        return Message(code,CodeMessageData[code])

    def getAllInfo(self,session):
        allData = []
        try:
            list = session.query(User).all()
            part = {}
            code = 0000
            for each in list:
                part = {}
                part['id']=each.id
                part['job_number'] = each.job_number
                part['user_name'] = each.user_name
                part['access_level'] = each.access_level
                allData.append(part)
        except:
            code=1004
        return Message(code,CodeMessageData[code],data=allData)


    def getDataByIdList(self,session,idList):
        """
        update data
        :param session: session
        :param newData: newData
        :return:
        """
        code=0000
        data=[]
        try:
            for one in idList:
                part =session.query(User).filter(User.id == one).one()
                data.append(part.toDict())
        except Exception as ex:
            # code=1003
            code=1028
            data=[]
        return Message(code,CodeMessageData[code],data)